/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.eleric.rememome.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.Toast;
import com.eleric.rememome.R;
import com.eleric.rememome.db.MemoDbAdaptor;
import com.eleric.rememome.memo.Memo;
import com.eleric.rememome.memo.MemoCategory;
import com.eleric.rememome.type.MemoStatusType;
import java.util.Calendar;

/**
 *
 * @author MWaldman
 */
public class EditActivity extends AddActivity
{

    protected String text;
    protected int memoId;
    protected Memo editItem;

    @Override
    public void onCreate(Bundle icicle)
    {
        super.onCreate(icicle);
        Bundle extras;
        extras = getIntent().getExtras();
//        editItem = (Memo) extras.get(AddActivity.MEMO_REF);
        memoId = extras.getInt(AddActivity.MEMO_REF);
        MemoDbAdaptor dbAdapter;
        dbAdapter = dbAdapterFactory.createDbAdapter(this);
        dbAdapter.open();
        editItem = dbAdapter.getMemo(memoId);

        editText.setText(editItem.getMemo());
        okButton.setText("EDIT");
        seekBar.setProgress(editItem.getPriority());
        priorityView.setText((new Integer(editItem.getPriority())).toString());
        for (int i = 0; i < rGroup.getChildCount(); i++)
        {
            View v = rGroup.getChildAt(i);
            Button b = (Button) v;
            if (editItem.getCategory().getShortName().equals(b.getText()))
            {
                rGroup.check(b.getId());
            }
        }
    }

    @Override
    public void addItem(View view)
    {
        editItem();
    }

    @Override
    public void clearItem(View view)
    {
        finish();
    }

    private void editItem()
    {
        MemoDbAdaptor dbAdapter;
        RadioButton rb;
        int rbIndex;
        String memoName;
        MemoCategory cat;
//        int priority;
        rbIndex = rGroup.getCheckedRadioButtonId();
        rb = (RadioButton) rGroup.findViewById(rbIndex);
//        priority =  R.integer.priority_default;
        memoName = editText.getText().toString();
        if (!memoName.trim().equals(""))
        {
//        cat = currentSelectedCategory;
        cat = (MemoCategory) rb.getTag();
        Calendar now = Calendar.getInstance();
        dbAdapter = dbAdapterFactory.createDbAdapter(this);
        dbAdapter.open();
        // Insert with default priority for now
        dbAdapter.updateMemo(memoId, memoName, cat.getId(), MemoStatusType.OPEN.getStatusId(),
                editItem.getCreationTimestamp().getTime(), now.getTime(), seekBar.getProgress());
        Toast.makeText(EditActivity.this, "Edited: " + editText.getText(),
                Toast.LENGTH_SHORT).show();
//        editText.setText("");
//        seekBar.setProgress(getResources().getInteger(R.integer.priority_default));
//        refreshPriorityView();
        dbAdapter.close();
        finish();
        }
        else
        {
        Toast.makeText(EditActivity.this, "Can not leave memo blank.",
                Toast.LENGTH_SHORT).show();
        }
    }
}
